<script setup name="Subject">

import { list, delSubject, updateUserSubject } from '@/api/subject/subject'
import NewSubjectDlg from './components/NewSubjectDlg.vue'
const { proxy } = getCurrentInstance()
const router = useRouter()
const route = useRoute()

const showSearch = ref(true)
const loading = ref(true)
const subjectList = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const selectName = ref('')
const dialogVisible = ref(false)
const total = ref(0)
const dlgTitle = ref('')
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  subjectName: undefined,
  subjectCode: undefined
})
const subjectInfo = ref({})
const props = defineProps({
  isAuth: {
    type: Boolean,
    default: false
  },
  authIds: {
    type: String,
    default: ''
  }
})
const emits = defineEmits(['authSuccess'])
const getList = () => {
  loading.value = true
  list(queryParams.value).then(response => {
    subjectList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}
function resetQuery () {
  proxy.resetForm('queryRef')
  queryParams.value.pageNum = 1
}
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
  single.value = selection.length !== 1
  selectName.value = selection.map(item => item.subjectName)
}
const handleAdd = () => {
  dialogVisible.value = true
  dlgTitle.value = '新增专题'
  subjectInfo.value = {
    id: '',
    subjectName: '',
    subjectCode: '',
    remark: ''
  }
}
const handleDelete = (row) => {
  selectName.value = row.subjectName || selectName.value
  const infoIds = row.id || ids.value
  console.log(infoIds)
  proxy.$modal.confirm('是否确认删除专题名称为"' + selectName.value + '"的数据项?').then(function () {
    return delSubject(infoIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}
const addSuccess = () => {
  dialogVisible.value = false
  getList()
}
const handleUpdate = (row) => {
  subjectInfo.value = row
  dlgTitle.value = '修改专题'
  dialogVisible.value = true
}
const viewProject = (row) => {
  router.push({ path: '/subject/project-data/index/' + row.id + '/' + row.subjectName })
}
const handleAuthSubject = async () => {
  if (ids.value.length === 0) {
    return proxy.$message.warning('请勾选专题')
  }
  const idsStr = ids.value.join(',')
  let result
  if (props.authIds) {
    result = Array.from(new Set(props.authIds.split(',').concat(idsStr.split(',')))).join(',')
  } else {
    result = idsStr
  }
  const params = {
    userId: route.params && route.params.userId,
    subjectIds: result
  }
  const res = await updateUserSubject(params)
  emits('authSuccess')
  proxy.$message.success('授权成功')
}
onMounted(() => {
  getList()
})
</script>

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        label="专题名称"
        prop="subjectName"
      >
        <el-input
          v-model="queryParams.subjectName"
          placeholder="请输入专题名称"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="专题编号"
        prop="subjectCode"
      >
        <el-input
          v-model="queryParams.subjectCode"
          placeholder="请输入专题编号"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button
          icon="Refresh"
          @click="resetQuery"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-row
      v-if="!isAuth"
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          @click="handleAdd"
          v-hasPermi="['project:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:delete']"
        >
          删除
        </el-button>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @query-table="getList"
      />
    </el-row>
    <el-row
      v-else
      :gutter="10"
      class="mb8"
    >
      <el-button
        type="primary"
        plain
        icon="Check"
        @click="handleAuthSubject"
      >
        授权访问
      </el-button>
    </el-row>
    <el-table
      ref="logininforRef"
      v-loading="loading"
      :data="subjectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="专题名称"
        align="center"
        prop="subjectName"
      />
      <el-table-column
        label="专题编号"
        align="center"
        prop="subjectCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        prop="opt"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <div v-if="!isAuth">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['project:edit']"
            >
              修改
            </el-button>
            <el-button
              link
              type="warning"
              icon="Files"
              @click="viewProject(scope.row)"
              v-hasPermi="['project:link']"
            >
              关联任务
            </el-button>
            <el-button
              link
              type="danger"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['project:delete']"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <NewSubjectDlg
      v-model:dialogFormVisible="dialogVisible"
      :title="dlgTitle"
      :subject-info="subjectInfo"
      @on-success="addSuccess"
    />
  </div>
</template>

<style scoped lang="scss">

</style>
